package com.macaline.technology.redis;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class RedisManager {
	
	/**
	 * 
	 * Description: 在组件初始化是进行类内组件的实体化
	 */ 
	public RedisManager() {
	}
	
	@Autowired
	private RedisTemplate<String, Object> objectRedisTemplate;
	
	@Autowired
	@SuppressWarnings("unused")
	private StringRedisTemplate stringRedisTemplate;
	
	private ValueOperations<String, String> stringOpsForValue;
	
	private ValueOperations<String, Object> objectOpsForValue;
	
	
	
	
	/**
	 * 
	* <p>@author warm_joy </p>
	* <p>Description: 加入一个String类型的缓存信息</p>
	* <p>Date: 2019-06-20 17:57</p>
	* @param key 缓存的key值
	* @param value 缓存的value值
	 */
	public void putString(String key,String value) {
		stringOpsForValue.set(key, value);
	}
	
	/**
	 * 
	* <p>@author warm_joy </p>
	* <p>Description: 设置一个具有存活时间的String类型缓存数据</p>
	* <p>Date: 2019-06-20 18:16</p>
	* @param key
	* @param value
	* @param expire
	 */
	public void putExpireString(String key,String value,Long expire) {
		stringOpsForValue.set(key, value);
		objectRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
	}
	
	/**
	 * 
	* <p>@author warm_joy </p>
	* <p>Description: 根据key值获取缓存中的String类型信息</p>
	* <p>Date: 2019-06-20 17:58</p>
	* @param key 获取缓存中数据的key值
	* @return 缓存中的信息 如果缓存中该数据返回null
	 */
	public String getString(String key) {
		return stringOpsForValue.get(key);
	}
	
	/**
	 * 
	* <p>@author warm_joy </p>
	* <p>Description: 加入一个Object类型的缓存信息</p>
	* <p>Date: 2019-06-20 18:05</p>
	* @param key
	* @param value
	 */
	public void putObject(String key,Object value) {
		objectOpsForValue.set(key, value);
	}
	
	/**
	 * 
	* <p>@author warm_joy </p>
	* <p>Description: 根据key值在缓存中查询一个Object类型</p>
	* <p>Date: 2019-06-20 18:06</p>
	* @param key 需要查询的对象的key值
	* @return
	 */
	public Object getObject(String key) {
		return objectOpsForValue.get(key);
	}
	
	

}
